#!/bin/bash
#################tomcat源码安装##################
#wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.69/bin/apache-tomcat-7.0.69.tar.gz
#定义Tomcat安装包文件路径
tomcatPackage=apache-tomcat-7.0.57
#安装路径
installPath=/usr/local/tomcat

#解压安装
tar zxvf ${tomcatPackage}.tar.gz

mkdir -p ${installPath};
installPath=${installPath}/${tomcatPackage}
mv ${tomcatPackage} ${installPath}

cd ${installPath}/bin
tar zxvf commons-daemon-native.tar.gz
cd ${installPath}/bin/commons-daemon-1.0.15-native-src/unix
./configure
make
cp jsvc ${installPath}/bin

rm -rf /usr/bin/tomcat
ln -s ${installPath}/bin/daemon.sh /usr/bin/tomcat

#创建www用户并给予权限
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

#创建www用户并给予权限
chown -R www:www ${installPath}

echo '
<role rolename="manager-gui"/>
<user username="root" password="qiangmima" roles="manager-gui"/>
'

echo "vi ${installPath}/bin/daemon.sh";
echo "
TOMCAT_USER=www
JAVA_HOME=$JAVA_HOME

chmod a+x ${installPath}/bin/daemon.sh
tomcat start
tomcat stop
tomcat run
tomcat version
"

/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

#设置开机自启动
echo "
export JAVA_HOME=$JAVA_HOME
/bin/sh ${installPath}/bin/startup.sh
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
" >> /etc/rc.d/rc.local

chmod x ${installPath}/bin/daemon.sh
chmod x ${installPath}/bin/startup.sh
chmod x ${installPath}/bin/shutdown.sh
chmod +x /etc/rc.d/rc.local


echo "finish"
